﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using Database.Context;
using QuanLyThongTinCoSo.Common;
using Model = Database.Models;


namespace QuanLyThongTinCoSo.Biz
{
    internal class CoQuanQuanLyPCCCBiz : BaseBiz
    {
        public CoQuanQuanLyPCCCBiz()
        {
            DbContext = new Context();
        }

        public CoQuanQuanLyPCCCBiz(Context context)
        {
            DbContext = context;
        }

        public override void Insert(Model.BaseModel model)
        {
            var item = model as Model.CoQuanQuanLyPCCC;
            item.Created = DateTime.Now;
            item.Modified = DateTime.Now;
            item.CreatedBy = Utilities.CurrentUser();
            item.ModifiedBy = Utilities.CurrentUser();

            DbContext.CoQuanQuanLyPCCCs.Add(model as Model.CoQuanQuanLyPCCC);
            DbContext.SaveChanges();
        }

        public override void Update(Model.BaseModel model)
        {
            var item = model as Model.CoQuanQuanLyPCCC;
            item.Modified = DateTime.Now;
            item.ModifiedBy = Utilities.CurrentUser();

            DbContext.Entry(item).State = (EntityState) System.Data.EntityState.Modified;
            DbContext.SaveChanges();
        }

        public override void Delete(Model.BaseModel model)
        {
            DbContext.CoQuanQuanLyPCCCs.Attach(model as Model.CoQuanQuanLyPCCC);
            DbContext.CoQuanQuanLyPCCCs.Remove(model as Model.CoQuanQuanLyPCCC);
            DbContext.Entry(model).State = EntityState.Deleted;

            DbContext.SaveChanges();
        }

        public override void DeleteItems(List<Model.BaseModel> models)
        {
            throw new NotImplementedException();
        }

        public override Model.BaseModel GetItemById(int id)
        {
            var model = (from item in DbContext.CoQuanQuanLyPCCCs
                         where item.Id == id
                         select item).First();
            return model;
        }

        #region GetAll

        public List<Model.CoQuanQuanLyPCCC> GetAll()
        {
            return DbContext.CoQuanQuanLyPCCCs.OrderBy(item => item.Ten).ToList();
        }

        #endregion
    }
}